package at.technikum.fotoalbum.beans;

import java.util.ArrayList;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import at.technikum.fotoalbum.database.DatabaseConnector;
import at.technikum.fotoalbum.utils.Utils;

public class SearchBean {

	private String searchedPicture;
	private String searchPictureInput;
	private ArrayList<Image> allMyPictures;

	private String searchedUser;
	private String searchUserInput;
	private ArrayList<Person> allUser;

	public ArrayList<Image> getAllMyPictures() {
		return allMyPictures;
	}

	public void setAllMyPictures(ArrayList<Image> allMyPictures) {
		this.allMyPictures = allMyPictures;
	}

	public String getSearchPictureInput() {
		return searchPictureInput;
	}

	public void setSearchPictureInput(String searchPictureInput) {
		this.searchPictureInput = searchPictureInput;
	}

	public String getSearchedPicture() {
		if (searchPictureInput == null || searchPictureInput.equals("")) {
			return null;
		}
		Image pic = ((DatabaseConnector) Utils.getManagedBean("databaseConnector"))
				.getImageFromImagename(searchPictureInput);
		if (pic.getPath() != null && !pic.getPath().equals("")) {
			searchedPicture = pic.getPath();
			return searchedPicture;
		} else {
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Bild konnte nicht gefunden werden.", null));
			return "/pages/userSearch.xhtml";
		}

	}

	public void setSearchedPicture(String searchedPicture) {
		this.searchedPicture = searchedPicture;
	}

	public String loadAllPictures() {
		SessionBean sb = (SessionBean) Utils.getManagedBean("sessionBean");
		String nick = sb.getNick();
		DatabaseConnector dc = (DatabaseConnector) Utils.getManagedBean("databaseConnector");
		ArrayList<Image> al = dc.getImagesFromUser(nick);
		for (Image image : al) {
			image.setPath(image.getPath().replace("/pictures/", "/pictures/thumbnail_"));
		}
		allMyPictures = al;
		return null;

	}

	/**
	 * @param searchedUser the searchedUser to set
	 */
	public void setSearchedUser(String searchedUser) {
		this.searchedUser = searchedUser;
	}

	/**
	 * @return the searchedUser
	 */
	public String getSearchedUser() {
		if (searchUserInput == null || searchUserInput.equals("")) {
			return null;
		}
		Person user = ((DatabaseConnector) Utils.getManagedBean("databaseConnector"))
				.getUserFromUsername(searchUserInput);
		if (user.getNick() != null && !user.getNick().equals("")) {
			searchedUser = user.getNick();
			return searchedUser;
		} else {
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "User konnte nicht gefunden werden.", null));
			return "/pages/userSearch.xhtml";
		}
	}

	/**
	 * @param searchUserInput the searchUserInput to set
	 */
	public void setSearchUserInput(String searchUserInput) {
		this.searchUserInput = searchUserInput;
	}

	/**
	 * @return the searchUserInput
	 */
	public String getSearchUserInput() {
		return searchUserInput;
	}

	/**
	 * @param allUser the allUser to set
	 */
	public void setAllUser(ArrayList<Person> allUser) {
		this.allUser = allUser;
	}

	/**
	 * @return the allUser
	 */
	public ArrayList<Person> getAllUser() {
		return allUser;
	}

}
